<template>
  <div>
    <div>
      <div>
          <!-- 添加课程类别的名称、介绍。类别 -->
        <el-input
          size="small"
          placeholder="名称"
          @keydown.enter.native="addPosition"
          v-model="lessons.name"
          style="width: 200px"
        >
        </el-input>
        <el-input
          size="small"
          placeholder="介绍"
          @keydown.enter.native="addPosition"
          v-model="lessons.intro"
          style="width: 300px"
        >
        </el-input>
        <el-select
          size="small"
          v-model="lessons.category"
          placeholder="类别"
          style="margin-left: 6px; margin-right: 6px"
        >
          <el-option
            v-for="item in categories"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
        <!-- 添加按钮 -->
        <el-button
          size="small"
          icon="el-icon-plus"
          type="primary"
          @click="addPosition"
          >添加</el-button
        >
      </div>
      <!-- 搜索 -->
      <div style="display: flex; justify-content: center; margin-top: 10px">
        <el-input
          v-model="keywords"
          placeholder="搜索用户"
          prefix-icon="el-icon-search"
          style="width: 400px; margin-right: 10px"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="doSearch"
          >搜索</el-button
        >
      </div>
    </div>
    <!-- 操作中的修改，修改名称、介绍和分类 -->
    <div class="posManaMain">
      <el-table
        stripe
        border
        size="small"
        :data="
          lessonss.slice((currentPage - 1) * pagesize, currentPage * pagesize)
        "
        @selection-change="handleSelectionChange"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="编号" width="80"> 
        
        </el-table-column>
        <el-table-column prop="name" label="名称" width="200">
        </el-table-column>
        <el-table-column prop="intro" label="介绍" width="400">
        </el-table-column>
        <el-table-column prop="category" label="分类" width="150">
        </el-table-column>
        <!-- 修改操作 -->
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="showEditView(scope.row)"
              type="primary"
              >修改</el-button
            >
            <el-button size="mini" @click="docEditView" type="primary"
              >文档编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- <el-button
                type="danger"
                size="small"
                style="margin-top: 8px"
                :disabled="this.multipleSelection.length==0"
                @click="deleteMany()">批量删除</el-button> -->

    <!-- 修改框 -->
    <el-dialog title="编辑" :visible.sync="dialogVisible" width="30%">
      <table>
        <tr>
          <td><el-tag>名称</el-tag></td>
          <td>
            <el-input
              v-model="updatelessons.name"
              size="small"
              style="margin-left: 6px"
            ></el-input>
          </td>
        </tr>
        <tr>
          <td><el-tag>介绍</el-tag></td>
          <td>
            <el-input
              v-model="updatelessons.intro"
              size="small"
              style="margin-left: 6px"
            ></el-input>
          </td>
        </tr>
        <tr>
          <td><el-tag>类别</el-tag></td>
          <td>
            <el-select
              size="small"
              v-model="updatelessons.category"
              placeholder="类别"
              style="margin-left: 6px; margin-right: 6px"
            >
              <el-option
                v-for="item in categories"
                :key="item"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </td>
        </tr>
      </table>
      <!-- 当关闭修改框的时候提醒是否关闭和修改 -->
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button
          size="small"
          type="primary"
          @click="doUpdate(updatelessons.id)"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <div style="display: flex; justify-content: flex-end">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 40]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="lessonss.length"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
        pagess:{
            pag:1,
            size:10,
        },
      keywords: "",
      currentPage: 1, //初始页
      pagesize: 5, //    每页的数据
      lessons: {
        name: "",
        category: "",
        intro: "",
      },
      categories: [
        "视频课程/收费",
        "视频课程/免费",
        "教程手册",
        "体系课",
        "专栏手记",
      ],
      lessonss: [],
      dialogVisible: false,
      updatelessons: {
        name: "",
        category: "",
        intro: "",
      },
      multipleSelection: [],
    };
  },
  mounted() {
    this.initPositions();
  },
  methods: {
    handleSizeChange: function (size) {
      this.pagesize = size;
      console.log(this.pagesize); //每页下拉显示数据
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
      console.log(this.currentPage); //点击第几页
    },
    //搜索
    doSearch() {
      this.axios
        .get("http://localhost:3000/lessons?name=" + this.keywords)
        .then((response) => {
          this.lessonss = response.data;
          this.lessons.name = "";
          this.lessons.category = "";
          this.lessons.intro = "";
        });
    },
    sizeChange(size) {
      this.page.size = size;
      this.initPositions();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.initPositions();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //添加
    addPosition() {
      if (this.lessons.name && this.lessons.category && this.lessons.intro) {
        this.axios
          .post("http://localhost:3000/lessons/", this.lessons)
          .then((response) => {
            if (response) {
              this.initPositions();
            }
          });
      } else {
        this.$message.error("字段不能为空");
      }
    },
    initPositions() {
      this.axios
        .get("http://localhost:3000/lessons", { params: this.page })
        .then((response) => {
          this.lessonss = response.data;
          this.lessons.name = "";
          this.lessons.category = "";
          this.lessons.intro = "";
        });
    },
    showEditView(data) {
      this.dialogVisible = true;
      Object.assign(this.updatelessons, data);
    },
    docEditView() {
      this.$router.replace("/ebook");
    },
    doUpdate(id) {
      this.axios
        .put("http://localhost:3000/lessons/" + id, this.updatelessons)
        .then((response) => {
          if (response) {
            console.log(response.data);
            this.initPositions();
            this.dialogVisible = false;
          }
        });
    },
    handleDelete(data) {
      this.$confirm(
        "此操作将永久删除[" + data.name + "]文档, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.axios
            .delete("http://localhost:3000/lessons/" + data.id)
            .then((response) => {
              if (response) {
                this.initPositions();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
.addPosInput {
  width: 300px;
  margin-right: 8px;
}
.posManaMain {
  margin-top: 10px;
}
.updatePosInput {
  width: 200px;
  margin-left: 8px;
}
</style>